<?php
namespace app\SuperAdmin\controller;
use think\Db;
use think\Controller;
use think\Session;
class SowingmapController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('adminid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {   
        $sowing=db("sowingmap")
        ->order('sowingid asc')
        ->select();;
        $this->assign('list',$sowing);
        return $this->fetch('sowingmap');
    }
    public function add(){
        $pic = request()->file('pic');
        if (!empty($pic)) 
        {
            $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static/img/showimg');
            if ($info == false) {
                $this->$pic->getError();
            }
        $data=[
        'sowingtitle'=>input('title'),
        'sowingpic'=>$info->getSaveName(),
        ];
   
         if( Db::name("sowingmap")->insert($data)){
          $this->success('添加成功！');
        }
        else{
          $this->error('添加失败');
        }
        } 
        else 
        {
          $this->error('添加失败');
        }
        
    }
    public function delete(){
       $sowingid=input('id');
       $sowingmap=Db::name("sowingmap")->find($sowingid);
       $pic=$sowingmap["sowingpic"];
       $file=ROOT_PATH."public".DS."static".DS. 'img'.DS.'showimg'.DS.$pic;
       if (file_exists($file)) {
        unlink($file);
       }
       if (Db::table("sowingmap")->delete($sowingid)){
         $this->success("删除成功");
       }
       else{
         $this->error("删除失败"); 
       }
    }
}